/**
 * ===========================================
 * Project: gamehistory-service
 * ===========================================
 * Package: com.cyou.gamehistory.service
 * 
 * Copyright (c) 2012, CYOU All Rights Reserved.
 * ===========================================
 */
package com.cyou.gamehistory.service;

import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.cyou.framework.cache.memcached.SpyMemcachedClient;
import com.cyou.framework.mapper.JsonMapper;
import com.cyou.framework.util.DateUtility;
import com.cyou.gamehistory.entity.AppInfo;
import com.cyou.gamehistory.repository.AppInfoDao;
import com.cyou.gamehistory.util.ApplicationConstants;
import com.cyou.gamehistory.util.MemcachedObjectType;

/**
 * <p>AppInfoService， 只有一个AppInfo。</p>
 *
 * @since 1.0
 * @version 1.0 2012-12-11
 * @author zhangxingkai
 */
@Component
@Transactional(readOnly = true)
public class AppInfoService {
	
	private static final Logger logger = LoggerFactory.getLogger(AppInfoService.class);
	
	@Autowired
	private AppInfoDao appInfoDao;

	@Autowired
	private SpyMemcachedClient spyMemcachedClient;
	
	private final JsonMapper jsonMapper = JsonMapper.nonDefaultMapper();
	public static final String KEY = MemcachedObjectType.AppInfo.getPrefix() + ApplicationConstants.APP_KEY;
	
	/**
	 * 获取AppInfo，如果不存在，则创建并返回AppInfo
	 */
	public AppInfo getAppInfo() {
		
		AppInfo appInfo = null;
		String jsonString = spyMemcachedClient.get(KEY);
		
		if (jsonString == null) { // memcached中不存在该缓存配置
			appInfo = appInfoDao.getAppInfoByAppKey(ApplicationConstants.APP_KEY);
			if (appInfo == null) { // 数据库中没有该项配置，自动创建
				saveAppInfo();
				appInfo = appInfoDao.getAppInfoByAppKey(ApplicationConstants.APP_KEY);
			}
			
			// 校验非空，并存入缓存
			Validate.notNull(appInfo);
			jsonString = jsonMapper.toJson(appInfo);
			spyMemcachedClient.set(KEY, MemcachedObjectType.AppInfo.getExpiredTime(), jsonString);
		} else { // 从memcached中获取数据
			appInfo = jsonMapper.fromJson(jsonString, AppInfo.class);
		}
		
		return appInfo;
	}
	
	/**
	 * 创建AppInfo, 如果已存在，则不进行任何操作
	 */
	@Transactional
	public void saveAppInfo() {
		AppInfo appInfo = appInfoDao.getAppInfoByAppKey(ApplicationConstants.APP_KEY);
		
		if (appInfo == null) {
			appInfo = new AppInfo();
			
			appInfo.setAppKey(ApplicationConstants.APP_KEY);
			appInfo.setName(ApplicationConstants.APP_NAME);
			appInfo.setUserCount(0L);
			appInfo.setUseTimes(0L);
			appInfo.setCreateTime(DateUtility.now());
			
			appInfoDao.saveAppInfo(appInfo);
			logger.info("AppInfo创建成功");
		} else {
			logger.warn("错误的调用， AppInfo已经存在");
		}
		
		return;
	}
	
	/**
	 * 添加用户人数
	 */
	@Transactional
	public void addUserCount() {
		AppInfo appInfo = appInfoDao.getAppInfoByAppKey(ApplicationConstants.APP_KEY);
		
		long userCount = appInfo.getUserCount() + 1;
		long useTimes = appInfo.getUseTimes() + 1;
		appInfo.setUserCount(userCount);
		appInfo.setUseTimes(useTimes);
		appInfo.setUpdateTime(DateUtility.now());
		
		appInfoDao.updateAppInfo(appInfo);
		
		// 主动使缓存失效，保证调用getAppInfo时可以获取正确的数据
		spyMemcachedClient.delete(KEY);
		
		logger.debug("编年史用户数加1(使用次数也加1)");
	}
	
	/**
	 * 添加使用次数.
	 */
	@Transactional
	public void addUseTimes() {
		AppInfo appInfo = appInfoDao.getAppInfoByAppKey(ApplicationConstants.APP_KEY);
		
		long useTimes = appInfo.getUseTimes() + 1;
		appInfo.setUseTimes(useTimes);
		appInfo.setUpdateTime(DateUtility.now());
		
		appInfoDao.updateAppInfo(appInfo);
		
		// 主动使缓存失效，保证调用getAppInfo时可以获取正确的数据
		spyMemcachedClient.delete(KEY);
		
		logger.debug("编年史使用次数加1");
	}

}
